local Msg = require("core.Msg")
local ActDefine = require("activity.Define")
local DateActExcel = require("excel.DateActivity")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local ChongzhiXBackYExcel = DateActExcel.chongzhixbacky
local Human = require("core.Human")
local MailLogicFW =require("mail.MailLogicFW")
local Config = require("Config")
local DateActFW = require("activity.DateActFW")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local Lang = require("common.Lang")

RECHARGE_DBDATA = RECHARGE_DBDATA or nil

function init()
    RECHARGE_DBDATA = DateActFW.onGetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIXBACKY)
end

function onRecharge(humanuuid, humanname, rechargeNum, nosave, init)
    if not DateActFW.onCheckActIsRunning(ActDefine.OPERATEACT_TYPE_CHONGZHIXBACKY) then
        return
    end

    if not RECHARGE_DBDATA then
        return
    end

    if not init then
        if RECHARGE_DBDATA._status then
            return
        end
    end

    if not init then
        if RECHARGE_DBDATA[humanuuid] then
            RECHARGE_DBDATA[humanuuid][2] = (RECHARGE_DBDATA[humanuuid][2] or 0) + rechargeNum 
        else
            RECHARGE_DBDATA[humanuuid] = {humanname, rechargeNum, 0}
        end
    end

    if not nosave then
        DateActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIXBACKY, RECHARGE_DBDATA)
    end

    if not init then
        onProecss(Human.onlineUuid[humanuuid], humanuuid, 1)
    end
end

function onActEnd()
    RECHARGE_DBDATA = nil
    DateActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIXBACKY, RECHARGE_DBDATA)
end

function onActRunning()
    RECHARGE_DBDATA = DateActFW.onGetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIXBACKY)
    if not RECHARGE_DBDATA then
        RECHARGE_DBDATA = {}
    end

    for k, v in pairs(RECHARGE_DBDATA) do
        if type(v) == "table" then
            onRecharge(k, v[1], v[2], true, true)
        end
    end
end

function get_grid(itemID, itemNum, itemColor, itemBind)
	local grid = Grid.create(nil, itemID, itemNum, itemBind, ItemDefine.qualityOut[itemColor])
	return grid
end

function onProecss(human)
    if not human then
        return
    end
    local nRuleID = 0
    nRuleID = DateActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_CHONGZHIXBACKY)
    if not nRuleID or not ChongzhiXBackYExcel[nRuleID] then
        return
    end

    local nChongzhiXBackYConfig = ChongzhiXBackYExcel[nRuleID]

    local nSendMsg = Msg.gc.GC_DATEACT_CHONGZHIXBACKY_QUREY
    nSendMsg.giftlist[0] = 0
    nSendMsg.rechargepre = nChongzhiXBackYConfig.payx
    nSendMsg.rechargeback = nChongzhiXBackYConfig.backy
    nSendMsg.rechargenum = RECHARGE_DBDATA[human._id] and RECHARGE_DBDATA[human._id][2] or 0
    nSendMsg.algcnt= RECHARGE_DBDATA[human._id] and RECHARGE_DBDATA[human._id][3] or 0
    nSendMsg.remaincnt = onCanGetCnt(RECHARGE_DBDATA[human._id], nChongzhiXBackYConfig)
    nSendMsg.maxcnt = nChongzhiXBackYConfig.rewardcnt 
    nSendMsg.status = nSendMsg.remaincnt > 0 and 0 or 1

    nSendMsg.giftlist[0] = nSendMsg.giftlist[0] + 1
    local nRewardData = nSendMsg.giftlist[nSendMsg.giftlist[0]]
    local grid = get_grid(ItemDefine.ITEM_ID_YUANBAO, nSendMsg.rechargeback, nil, 0)
    Grid.makeItem(grid, nRewardData)

    if human then
        Msg.send(nSendMsg, human.fd)
    end
end

function onCanGetCnt(rechargeData, actData)
    if not rechargeData then
        return 0
    end
    local nPayXPre = actData.payx
    local nRechargeNum = rechargeData[2]
    local nAllCnt = (nRechargeNum > 0 and nPayXPre > 0) and math.floor(nRechargeNum / nPayXPre) or 0
    return nAllCnt > actData.rewardcnt and actData.rewardcnt - rechargeData[3] or nAllCnt - rechargeData[3]
end

function onGetReward(human)
    if not RECHARGE_DBDATA or not RECHARGE_DBDATA[human._id] then
        local msgErr = Msg.gc.GC_BROADCAST
        msgErr.type = Lang.MOUSE
        msgErr.msg = "活动期间暂无充值记录，无法领取" 
        Msg.send(msgErr, human.fd)
        return
    end

    local nRuleID = DateActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_CHONGZHIXBACKY)
    if nRuleID and ChongzhiXBackYExcel[nRuleID] then
        local nChongzhiXBackYConfig = ChongzhiXBackYExcel[nRuleID]
        if onCanGetCnt(RECHARGE_DBDATA[human._id], nChongzhiXBackYConfig) <= 0 then
            return
        end

        local nBackY = nChongzhiXBackYConfig.backy
        if nBackY then

            RECHARGE_DBDATA[human._id][3] = RECHARGE_DBDATA[human._id][3] + 1 
            DateActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIXBACKY, RECHARGE_DBDATA)

            local nSend2Ldata = {
                type = 7,
                data = {
                    backy = nBackY,
                }
            }
            InnerDataManagerFW.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_ACT, nSend2Ldata)
            onProecss(human)
        end
    end
end


DateActFW.onRegisterCallBack(ActDefine.OPERATEACT_TYPE_CHONGZHIXBACKY, nil, onActRunning, onActEnd, nil)

